/**
 * 类型断言：
 *    1. <>
 *    2. as
 */
let someValue: unknown = "this is a string";
// let strLength: number = (<string>someValue).length
// let strLength: number = (someValue as string).length;

// 强制断言，断言不允许时，可以先断言成一个 unknown
let strLength: boolean = (someValue as string).length as unknown as boolean;

// const app = document.querySelector("#app") as HTMLInputElement;

// app.value;

function myFunc(maybeString: string | null | undefined) {
  const str: string = maybeString; // 不能将类型“string | null | undefined”分配给类型“string”。不能将类型“undefined”分配给类型“string”
  const str1: string = maybeString!; // OK
  const str2: string = maybeString as string;

  console.log(str);
  console.log(str1);
}

type CallbackType = () => number;

function myFunc(callback?: CallbackType | undefined) {
  const num1 = callback(); // Error 不能调用可能是“未定义”的对象。
  const num2 = callback!(); // Ok
  const num3 = callback?.(); // OK 推荐

  if (callback) {
    // 类型收窄了
    const num4 = callback();
  }
}

myFunc();

let s1 = "hello"; // string
const s2 = "Hello"; // "Hello"
// 通过 as const 断言成 常量类型
let s3 = "hello" as const; // "hello"
